package view;

import java.awt.Button;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Label;
import java.awt.LayoutManager;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collections;
import java.util.List;

import components.AbstractComponent;
import components.Device;
import components.Form;

import device.DeviceHelper;

public class Render {

    private Device device;

    public Render() {
    // TODO Auto-generated constructor stub
    }

    public Render(Device device) {
        this.device = device;
    }

    public Button render(final components.Button com_Button, Container parent) {
        Button button = new Button();
        button.setLabel(com_Button.getText());
        button.setFont(new Font("Arial", Font.BOLD, 20));
        button.setForeground(Color.BLACK);
        button.setBackground(Color.GREEN);
        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                DeviceHelper.getInstance().executeCommand(com_Button.getAction(), com_Button, device);
            }
        });
        parent.add(button);
        return button;
    }

    public Label render(components.Label com_Label, Container parent) {
        //Trabalhando com o label principal 
        Label label = new Label();
        label.setText(com_Label.getText());
        label.setFont(new Font("Arial", Font.BOLD, 30));
        label.setForeground(Color.BLACK);

        parent.add(label);
        return label;
    }

    public Panel render(components.Panel com_panel, Container parent) {
        Panel panel = new Panel();
        panel.setBackground(Color.RED);
        //panel.setLocation(115, 45);
        //panel.setLayout(layoutManager);
        parent.add(panel);
        return panel;
    }

    public Button getButton(String text, Container parent, ActionListener actionListener) {
        Button button = new Button();
        //button.setSize(90, 40);
        button.setLabel(text);
        button.setFont(new Font("Arial", Font.BOLD, 20));
        button.setForeground(Color.BLACK);
        button.setBackground(Color.GREEN);
        button.addActionListener(actionListener);
        //button.setLocation((panel.getWidth() / 3) + 30, 150);
        parent.add(button);
        return button;
    }

    public Choice getCombo(List<String> values, Container parent) {
        if (values == null) {
            values = Collections.<String> emptyList();
        }
        Choice choice = new Choice();
        for (String value : values) {
            choice.addItem(value);
        }
        parent.add(choice);
        return choice;
    }

    public java.awt.List getComboList(List<String> values, Container parent) {
        if (values == null) {
            values = Collections.<String> emptyList();
        }
        java.awt.List choice = new java.awt.List();
        for (String value : values) {
            choice.add(value);
        }
        parent.add(choice);
        return choice;
    }

    public Label getLabel(String text, Container parent) {
        Label label = new Label();
        label.setText(text);
        //label1.setLocation((panel.getWidth() / 3) + 30, 5);
        label.setFont(new Font("Arial", Font.BOLD, 30));
        // label1.setAlignment(Label.CENTER);
        //label1.setBackground(Color.BLACK);
        label.setForeground(Color.BLACK);
        //label1.setSize(18 * text.length(), 40);
        parent.add(label);
        return label;
    }

    public Panel getPanel(LayoutManager layoutManager, Container parent) {
        Panel panel = new Panel();
        panel.setBackground(Color.RED);
        //panel.setLocation(115, 45);
        panel.setLayout(layoutManager);
        parent.add(panel);
        return panel;
    }

    public TextField getTextField(Container parent) {
        TextField text = new TextField();
        parent.add(text);
        return text;
    }

    public Container renderForm(Form form) {
        Panel panel = new Panel();
        panel.setSize(430, 250);
        panel.setBackground(Color.WHITE);
        panel.setLocation(135, 85);
        panel.setLayout(new FlowLayout());

        renderChindrens(form.getChildrens(), panel);

        return panel;

    }

    private void renderChindrens(List<AbstractComponent> list, Container parent) {
        for (AbstractComponent component : list) {
            if (component instanceof components.Button) {
                render((components.Button) component, parent);
            } else if (component instanceof components.Panel) {
                render(((components.Panel) component), parent);
            } else if (component instanceof components.Label) {
                render(((components.Label) component), parent);
            }

        }
    }

}
